// ignore_for_file: unnecessary_null_comparison, depend_on_referenced_packages, non_constant_identifier_names

import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:crypto/crypto.dart';
import 'package:verse_systems/net/src/sparrow_dio_config.dart';
import 'package:verse_systems/utils/aes_util.dart';
import 'dart:convert' as convert;

class SignUtil {
  //签名参数算法

  static String aes_key = '6jqvnd8SHT2u288w';
  static String aes_iv = 'abc1232IF866nji7';
  static String key = 'aJeE7zY27AoJEEIS6l8hRkSK5As9Fh2v';



  /*采用当前时间戳，精确到秒*/
  static String get timeStamp {
    return DateTime.now().millisecondsSinceEpoch.toString();
  }

  /*AES加密参数*/
  static String AESencryptAes({Map<String, dynamic>? queryParameters}) {
    String jsonString = convert.jsonEncode(queryParameters);
    debugPrint('请求签名参数jsonString:$jsonString');
    final String encryptedText = AESUtil.encryptAes(
        plainText: jsonString, keyStr: aes_key, ivStr: aes_iv);
    return encryptedText;
  }

  /*MD5加密签名*/
  static String generateMD5(String data) {
    final String signMD5 = key + data;
    Uint8List content = const Utf8Encoder().convert(signMD5);
    Digest digest = md5.convert(content);
    return digest.toString();
  }

  static Map<String, dynamic> encryptSign(
      {Map<String, dynamic>? queryParameters}) {
    final String enData = AESencryptAes(queryParameters: queryParameters);
    final String timeStamps = timeStamp;
    String encryptSign = generateMD5(timeStamps);


    debugPrint('请求签名参数:$queryParameters');
    debugPrint('请求签名时间戳:$timeStamps');

    // debugPrint('AES签名之后:$enData');
    // debugPrint('MD5签名之后:$encryptSign');
    
    return {
      "sign": encryptSign,
      "en_data": enData,
      "timestamp": timeStamps,
      "lang": SparrowDioConfig.language
    };
  }
}
